 package com.example.jchuah.tiles;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridLayout;


    public class TileGridLayout extends GridLayout {
        int numRows = 4;
        int numCols = 4;

        View[][] grid = new View[numRows][numCols];

        public TileGridLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            setupGrid();

        }

        public TileGridLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            setupGrid();
        }

        public TileGridLayout(Context context) {
            super(context);
            setupGrid();
        }

        protected void setupGrid() {
            numRows = getRowCount();
            numCols = getColumnCount();
            grid = new View[numRows][numCols];
        }


        public View extractTile(RowColPair pair) {
            View tile = grid[pair.row][pair.col];
            grid[pair.row][pair.col] = null;
            removeView(tile);
            return tile;
        }

        public void addTile(RowColPair pair, View tile) {
            addTile(pair.row, pair.col, tile);
        }

        public boolean isValidRowColPair(RowColPair pair) {
            return (pair.col >= 0 && pair.row >= 0 &&
                    pair.row < grid.length && pair.col < grid[0].length);
        }

        public void addTile(int row, int col, View tile) {
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams();
            layoutParams.rowSpec = GridLayout.spec(row);
            layoutParams.columnSpec = GridLayout.spec(col);
            tile.setLayoutParams(layoutParams);

            addView(tile);
            grid[row][col] = tile;
        }

        public RowColPair getRowColumn(View view) {
            RowColPair result = new RowColPair();
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == view) {
                        result.row = i;
                        result.col = j;
                        return result;
                    }
                }
            }
            return result;
        }

        public View getTileAt(RowColPair pair) {
            return grid[pair.row][pair.col];
        }

        public boolean isEmpty(RowColPair pair) {
            return grid[pair.row][pair.col] == null;
        }

        class RowColPair {
            int row = -1;
            int col = -1;

            public Object clone() {
                RowColPair theClone = new RowColPair();
                theClone.row = row;
                theClone.col = col;
                return theClone;
            }

            public RowColPair getBelow() {
                RowColPair result = new RowColPair();
                result.row = row + 1;
                result.col = col;
                return result;
            }

            public RowColPair getAbove() {
                RowColPair result = new RowColPair();
                result.row = row - 1;
                result.col = col;
                return result;
            }
            public RowColPair getLeft() {
                RowColPair result = new RowColPair();
                result.row = row;
                result.col = col - 1;
                return result;
            }
            public RowColPair getRight() {
                RowColPair result = new RowColPair();
                result.row = row;
                result.col = col + 1;
                return result;
            }
        }


    }






